﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation either version
 * 2 of the License, or (at your option) any later version.
 * 
 * ExtremeML is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System.Diagnostics;
using System.Drawing;
using ExtremeML.Packaging;
using ExtremeML.Spreadsheet.Address;

namespace ExtremeML.Samples
{
	public static class CommentManipulation
	{
		/// <summary>
		/// Inserts random text comments into designated cells in the Blank worksheet.
		/// </summary>
		public static void SetTextComments()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Blank"];

				for (var row = 0; row < 10; row++)
				{
					for (var col = 0; col < 10; col++)
					{
                        var target = new GridReference(row, col);
                        var comment = Utility.GetRandomString();
                        part.Worksheet.SetCommentText(target, comment);
					}
				}
			}
		}

		/// <summary>
		/// Inserts RichText comments into designated cells in the Blank worksheet.
		/// </summary>
		public static void SetRichTextComments()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Blank"];

				for (var row = 0; row < 10; row++)
				{
					for (var col = 0; col < 10; col++)
					{
						var target = new GridReference(row, col);
                        var comment = Utility.GetSampleRichText();
                        part.Worksheet.SetCommentRichText(target, comment);
					}
				}
			}
		}

        /// <summary>
		/// Inserts random text comments into designated cells in the Blank
		/// worksheet, overriding the default setting of the comment author name.
        /// </summary>
		public static void OverrideCommentAuthor()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Blank"];

				for (var row = 0; row < 10; row++)
				{
					for (var col = 0; col < 10; col++)
					{
						var target = new GridReference(row, col);
						var comment = Utility.GetRandomString();
						part.Worksheet.SetCommentText(target, comment, "myName");
					}
				}
			}
		}

		/// <summary>
		/// Inserts random text comments into designated cells in the Blank
		/// worksheet, overriding the default setting of the comment rectangle size.
		/// </summary>
		public static void OverrideCommentShapeProperties()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Blank"];

				for (var row = 0; row < 10; row++)
				{
					for (var col = 0; col < 10; col++)
					{
						var target = new GridReference(row, col);
						var comment = Utility.GetRandomString();
						part.Worksheet.SetCommentText(target, comment, null, new Size(200, 200));
					}
				}
			}
		}

		/// <summary>
		/// Inserts an image into a cell comment, rendered at its natural size.
		/// </summary>
		public static void SetImageComment()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Blank"];
				var image = Utility.GetRandomImage();
				part.Worksheet.SetCommentImage(new GridReference(0, 0), image, null, null);
			}
		}

		/// <summary>
		/// Inserts an image into a cell comment, sized to render at the specified width.
		/// </summary>
		public static void SetImageCommentSizedWidth()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Blank"];
				var image = Utility.GetRandomImage();
				part.Worksheet.SetCommentImage(new GridReference(0, 0), image, 100, null);
			}
		}

		/// <summary>
		/// Inserts an image into a cell comment, sized to render at the specified height.
		/// </summary>
		public static void SetImageCommentSizedHeight()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Blank"];
				var image = Utility.GetRandomImage();
				part.Worksheet.SetCommentImage(new GridReference(0, 0), image, null, 100);
			}
		}

		/// <summary>
		/// Inserts an image into a cell comment, sized to render at the specified width and height.
		/// </summary>
		public static void SetImageCommentSizedWidthAndHeight()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Blank"];
				var image = Utility.GetRandomImage();
				part.Worksheet.SetCommentImage(new GridReference(0, 0), image, 100, 200);
			}
		}

        /// <summary>
		/// Inserts random text comments into designated cells in
		/// the Blank worksheet and reads them back to verify them.
        /// </summary>
		public static void SetAndReadTextComments()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Blank"];

				for (var row = 0; row < 10; row++)
				{
					for (var col = 0; col < 10; col++)
					{
						var comment = Utility.GetRandomString();
						var target = new GridReference(row, col);
						part.Worksheet.SetCommentText(target, comment);
						Debug.Assert(comment == part.Worksheet.GetCommentText(target));
					}
				}
			}
		}
    }
}
